package com.itany.boot.controller;

import com.github.pagehelper.PageInfo;
import com.itany.boot.constant.ResponseCodeConstant;
import com.itany.boot.entity.Resource;
import com.itany.boot.exception.RequestParameterErrorException;
import com.itany.boot.service.ResourceService;
import com.itany.boot.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 黄衡熙
 * @version 1.0
 */
//是 @Controller + @ResponseBody 的组合注解。
@RestController
@RequestMapping("/resource")
public class ResourceController {
    @Autowired
    private ResourceService resourceService;

    @RequestMapping("/findByParams")
    public ModelAndView findByParams(Resource resourceParam,String pageNo,String pageSize){
        ModelAndView mav=new ModelAndView();
        PageInfo<Resource> resourcePage=resourceService.findByParams(resourceParam,pageNo,pageSize);
        mav.addObject("resourcePage",resourcePage);
        mav.setViewName("back_resourceSet");
//        带回前端
        mav.addObject("resourceParam",resourceParam);
        return mav;
    }

    @RequestMapping("/modifyStatus")
    public ResponseResult modifyStatus(String id,String status){
        ResponseResult result=new ResponseResult();
        try {
            resourceService.modifyStatus(id,status);
            result.setMessage("修改成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (RequestParameterErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        }catch (Exception e) {
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }

    @RequestMapping("/detail")
    public ModelAndView detail(String id) {
        ModelAndView mav = new ModelAndView();
        try {
            // 这里需要根据id查询资源详情
            Resource resource = resourceService.findById(id);
            mav.addObject("resource", resource);
            mav.setViewName("back_resourceDetailSet");
        } catch (Exception e) {
            mav.setViewName("error");
            mav.addObject("errorMsg", "资源不存在");
        }
        return mav;
    }

    @RequestMapping("/download")
    public void downloadResource(String id, HttpServletResponse response) {
        try {
            resourceService.downloadResource(id, response);
        } catch (Exception e) {
            // 处理下载异常
            try {
                response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "下载失败: " + e.getMessage());
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
        }
    }
}
